﻿using System;
using System.Collections.Generic;
using System.Linq;
using static PengSW.RuntimeLog.RL;

namespace PengSW.TimerTaskHelper
{
    public static class TimerTaskManager
    {
        public static void AddTimerTask(TimeSpan aInterval, Action aAction, string aDescription)
        {
            TimerTask aTimerTask = TimerTasks.FirstOrDefault(r => r.Description == aDescription);
            if (aTimerTask == null)
            {
                L($"预约[{aDescription}]");
                aTimerTask = new TimerTask(aInterval, aAction, aDescription);
                TimerTasks.Add(aTimerTask);
            }
            aTimerTask.Reset();
        }

        public static List<TimerTask> TimerTasks = new List<TimerTask>();

        public static void RemoveTask(TimerTask aTimerTask)
        {
            if (aTimerTask == null) return;
            try
            {
                TimerTasks.Remove(aTimerTask);
            }
            catch (Exception ex)
            {
                L($"Remove Timer Task [{ex.Message}]");
            }
        }

        public static void StopTask(string aDescription)
        {
            TimerTasks.FirstOrDefault(r => r.Description == aDescription)?.Stop();
        }
    }
}
